package ca.etsmtl.log320;

public class EvaluationLau {

	// Constantes des valeurs associees a chaque condition
		private final static int PIECEVALUE = 15;
		private final static int DANGER = 10;
		private final static int PROTECT = 5;
		private final static int FREE_COLUMN = 15;
		private final static int EAT = 11;
		private final static int PROGRESS = 9;
		private final static int HLINE = 1;
		private final static int WIN = 10000;
		
		

		public static void main(String[] args) {
			getBoardValue((new Board(new int[][] { 
					{2,2,0,0,0,0,4,4},
					{2,2,0,0,0,0,4,4}, 
					{2,2,0,0,0,0,4,4},
					{2,2,0,0,0,0,4,4}, 
					{2,2,0,0,0,0,4,4},
					{2,2,0,0,0,0,4,4}, 
					{2,2,0,0,0,0,4,4},
					{2,2,0,0,0,0,4,4}, 
				}).get()), Consts.CASE_NOIR);
		}

		public static int getBoardValue(long[] array, int playerColor) {
				int value = 0;
				boolean playerHasFreeColumn = false;
				boolean opponentHasFreeColumn = false;
		
				for (int x = 0; x < 8; x++) {
					playerHasFreeColumn = true;
					opponentHasFreeColumn = true;
		
					for (int y = 0; y < 8; y++) {
						int pieceColor = Board.get(array, x, y);
		
						if (pieceColor == Consts.CASE_VIDE)
							continue;
		
						// On additionne la valeur de la piece si elle appartient au
						// joueur en cours
						if (pieceColor == playerColor) {
							value += getPieceValue(array, x, y, pieceColor, playerColor);
							playerHasFreeColumn = false;
						}
						// On soustrait si c'est la piece de l'adversaire
						else {
							value -= getPieceValue(array, x, y, pieceColor, playerColor);
							opponentHasFreeColumn = false;
						}
					}
					if (playerHasFreeColumn) {
						value -= FREE_COLUMN;
		
					}
					if (opponentHasFreeColumn) {
						value += FREE_COLUMN;
					}
				}
		
//				if(Consts.DEBUG) Evaluation.printBoardAndValue(array, value, playerColor);
				return value;
		}

		private static int getPieceValue(long[] array, int x, int y,
				int pieceColor, int playerColor) {
			int value = PIECEVALUE;
			boolean leftBound = x == 0;
			boolean rightBound = x == 7;

			if (pieceColor == Consts.CASE_NOIR) {
				value += y;
				if (y == 7)
					value += WIN; // NOIR GAGNE
				else {
					if (!leftBound) {
						value += getAttackPositionValue(Board.get(array, x - 1, y + 1), pieceColor, playerColor, y);
					}
					if (!rightBound) {
						value += getAttackPositionValue(Board.get(array, x + 1, y + 1), pieceColor, playerColor, y);
					}
				}
			} else {
				value += 7 - y;
				if (y == 0)
					value += WIN; // BLANC GAGNE
				else {
					if (!leftBound) {
						value += getAttackPositionValue(Board.get(array, x - 1, y - 1), pieceColor, playerColor, y);
					}
					if (!rightBound) {
						value += getAttackPositionValue(Board.get(array, x + 1, y - 1), pieceColor, playerColor, y);
					}
				}
			}

			if (!leftBound && !rightBound
					&& isHLine(Board.get(array, x - 1, y), Board.get(array, x + 1, y), pieceColor)) {
				value += HLINE;
			}

			return value;
		}

		private static int getAttackPositionValue(int attackPositionColor,
				int pieceColor, int playerColor, int y ) {
			int value = 0;


				if (attackPositionColor == Consts.CASE_VIDE) {
					value = evaluationRow(playerColor, DANGER, y);
				} else if (attackPositionColor == pieceColor) {
					value = evaluationRow(playerColor,PROTECT, y);
				
				} else if (pieceColor == playerColor) {
					value = evaluationRow(playerColor,DANGER, y);
				}else if ( pieceColor != playerColor){
					value = evaluationRow(playerColor, EAT, y);
				}

			return value;
		}

		// Valeur supplementaire si on a une ligne horizontale de 3+ pieces du meme
		// joueur
		private static boolean isHLine(int HLinePosition1, int HLinePosition2, int pieceColor) {
			return (pieceColor == HLinePosition1 && pieceColor == HLinePosition2);
		}
	
		public static int evaluationRow( int playerColor,int value, int y){
			if(playerColor == Consts.CASE_NOIR){
				value = (value * (y+1));
			}
			else{
				value = (value * 8)/(y+1);
			}
			return value;
		}
}
